home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / gencodec / source / writeexternalfile.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  102 lines

  1. #include "WriteExternalFile.h"
  2. #include "Tools.h"
  3. #include "MB_protos.h"
  4. #include "MB_pragmas.h"
  5. #include "MB.h"
  6.  
  7. #include <exec/types.h>
  8. #include <proto/dos.h>
  9. #include <proto/exec.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. /****************************************************************************************************************/
  15. /*****                                                                                                        *****/
  16. /**                                         WriteExternalFile                                                       **/
  17. /*****                                                                                                        *****/
  18. /****************************************************************************************************************/
  19.  
  20. /* Create a file where are the external variables and functions declarations */
  21. BOOL WriteExternalFile(char *Externals,ULONG varnb)
  22. {
  23.     int                        i;
  24.     BPTR                    TMPlock;
  25.     ULONG                    length, type;
  26.     char                    *adr_file = NULL;
  27.     struct FileInfoBlock    *Info;
  28.     BOOL                    bool_aux = FALSE;
  29.     char                    *varname;
  30.     char                    *tmp;
  31.     BOOL                    result = FALSE;
  32.     FILE                    *file;
  33.     BOOL                    ExternalExist=FALSE;
  34.  
  35.     /* If the file already exists, we load it in memory */
  36.     adr_file = LoadFileInRAM(Externals,FALSE);
  37.  
  38.     for(i=0;!ExternalExist && i<varnb;i++)
  39.     {
  40.         MB_GetVarInfo(i,MUIB_VarType,&type,TAG_END);
  41.         ExternalExist=(type==TYPEVAR_EXTERNAL || type==TYPEVAR_HOOK);
  42.     }
  43.  
  44.     if (ExternalExist && (file = fopenFile(Externals, "a+", FALSE)))
  45.     {
  46.         if (!adr_file)
  47.             fprintf(file,"#include <utility/hooks.h>\n\n");
  48.  
  49.         for(i=0;i<varnb;i++)
  50.         {
  51.             MB_GetVarInfo (i,
  52.                               MUIB_VarType, &type,
  53.                               MUIB_VarName, &varname,
  54.                            TAG_END
  55.                            );
  56.             switch(type)    /* if the declaration does not exist, we generate it */
  57.             {
  58.                 case TYPEVAR_EXTERNAL:
  59.                     tmp=AllocMemory(strlen(varname)+14,TRUE);
  60.                     strcpy(tmp,"extern int ");
  61.                     strcat(tmp,varname);
  62.                     strcat(tmp,";\n");
  63.                     if (adr_file)
  64.                         bool_aux = (strstr(adr_file,tmp)!=NULL);
  65.                     if (!bool_aux)
  66.                         fprintf(file,tmp);
  67.                     FreeMemory(tmp);
  68.                     break;
  69.  
  70.                 case TYPEVAR_HOOK:
  71.                     tmp=AllocMemory(strlen(varname)+52,TRUE);
  72.                     strcpy(tmp,"APTR ");
  73.                     strcat(tmp,varname);
  74.                     strcat(tmp,"( struct Hook *a0, APTR a2, APTR a1 );\n");
  75.                     if (adr_file)
  76.                         bool_aux = (strstr(adr_file,tmp)!=NULL);
  77.                     if (!bool_aux)
  78.                         fprintf(file,tmp);
  79.                     FreeMemory(tmp);
  80.                     break;
  81.             }
  82.         }
  83.         fcloseFile(file);
  84.     }
  85.     if (adr_file)
  86.         FreeMemory(adr_file);
  87.  
  88.     if (TMPlock = Lock(Externals,ACCESS_READ))    /* if the file is 0 bytes long : we remove it */
  89.     {
  90.         Info = AllocMemory(sizeof(struct FileInfoBlock),TRUE);
  91.         Examine(TMPlock, Info);
  92.         UnLock(TMPlock);
  93.         length = Info->fib_Size;
  94.         FreeMemory(Info);
  95.         if (length == 0)
  96.             DeleteFile(Externals);
  97.         else
  98.             result = TRUE;
  99.     }
  100.     return(result);
  101. }
  102.